<?php
class Mageex_AdvanceSeo_Model_Tags extends Mage_Tag_Model_Tag
{
	public function getTaggedProductsUrl()
    {
        if(Mage::helper('advanceseo/config')->isEnableRewriteTags()){
			$urlrewriteModel 	= Mage::getModel('core/url_rewrite');
			/*
            $tagCollection 		= Mage::getModel('tag/tag')->getCollection();
            $dataRewrite 		= array();
            foreach ($tagCollection as $item) {
                  $oldUrl 		= 'tag/product/list/tagId/'.$item->getId();
				  $requestPath 	= Mage::helper('advanceseo/config')->getPathTag().
					  								 '/'.
													 Mage::helper('advanceseo/data')->stringConvert($item->getName()).
					  								 Mage::helper('advanceseo/data')->getSuffix();
						//filter old target_path
                  $rewriteCollection = $urlrewriteModel->getCollection()->addFieldToFilter('target_path', $oldUrl);
                  if(count($rewriteCollection)==0){
                      $dataRewrite['store_id'] = Mage::app()->getStore()->getId();
                      $dataRewrite['id_path'] = Mage::helper('advanceseo/config')->getPathTag().'/'.$item->getId();
                      $dataRewrite['target_path'] = $oldUrl;
                  	  $dataRewrite['request_path'] = $requestPath; 
							//@ save url here
                      $dataRewrite['is_system'] = 0;
                      $urlrewriteModel->setData($dataRewrite);
                      $urlrewriteModel->save();
                 }else{
					 
					foreach($rewriteCollection as $rewrite){
						$idCoreUrl = $rewrite->getId();
						$requestPaths = $rewrite->getRequestPath();
						break;
					}
					if($requestPaths  != $requestPath){
						$data['request_path'] = $requestPath;
						$urlrewriteModel->load($idCoreUrl)->addData($data);
						$urlrewriteModel->setId($idCoreUrl)->save();
					}
				 }
            }
            unset($tagCollection);
			*/
        	return Mage::getBaseUrl().Mage::helper('advanceseo/config')->getPathTag().
        		   '/'.
            	   Mage::helper('advanceseo/data')->stringConvert($this->getName()).
            	   Mage::helper('advanceseo/data')->getSuffix();
        }
        else
            return Mage::getUrl('tag/product/list', array('tagId' => $this->getTagId()));;
    }
}